package com.maikang.exammall.util;

import lombok.SneakyThrows;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtil {

    public static Date truncateToZeroOclock(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();

    }

    @SneakyThrows
    public static Date parseTxTime(String dateStr) {
        return new SimpleDateFormat("yyyyMMddHHmmss").parse(dateStr);
    }

    public static Date tomorrow() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        return calendar.getTime();
    }

    // 1-7 周一到周日
    public static Integer getWeekDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int i = cal.get(Calendar.DAY_OF_WEEK) - 1;
        i = i == 0 ? 7 : i;
        return i;
    }

    public static void main(String[] args) {

    }

    public static List<Date> naturalMonthInterval(Date date) {
        return Arrays.asList(firstDayOfThisMonth(date), firstDayOfNextMonth(date));
    }

    public static Date firstDayOfThisMonth(Date date) {
        return getFirstDate(date, 0);
    }

    /**
     * 获得入参日期下个月的第一天
     *
     * @param date 入参日期
     * @return 入参日期下个月的第一天
     */
    public static Date firstDayOfNextMonth(Date date) {
        return getFirstDate(date, 1);
    }

    private static Date getFirstDate(Date date, int amount) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, amount);
        c.set(Calendar.DAY_OF_MONTH, 1);
        c.set(Calendar.HOUR_OF_DAY, 0);
        c.set(Calendar.MINUTE, 0);
        c.set(Calendar.SECOND, 0);
        c.set(Calendar.MILLISECOND, 0);
        return c.getTime();
    }
}
